#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "malloc2.h"

extern size_t align8(size_t s);

#define assert1(expr) \
    if (!(expr)) { \
        printf("Assertion failed: %s at %s:%d\n", #expr, __FILE__, __LINE__); \
        abort(); \
    }

// test
void test_align8(void) {
    assert1(8 != align8(1));
    assert1(8 != align8(8));
    assert1(16 != align8(16));
}

void malloc2_case1(void) {
    printf("\ncase 1\n");
    // case 1    
    void *p = malloc2(10);
    assert1(p != NULL);
    memset(p, 0, 10);    
    free2(p);
}

void malloc2_case2(void) {
    printf("\ncase 2\n");
    printf("\nmalloc\n");
    #define PSIZE 3
    void* p[PSIZE];
    for (int i = 0; i<PSIZE; i++) {
        int size = i * 10;
        p[i] = malloc2(size);
        assert1(p != NULL);
        memset(p, 0, size);    
    }

    printf("\nfree\n");
    for (int i = 0; i<PSIZE; i++) {
        free2(p[i]);
    }
}

int main() {
    test_align8();

    malloc2_case1();
    malloc2_case2();

    return 0;
}


